/*******************************************************************************
 * File name:     main.c
 * Description:   HTX project- it's a basic framework for LPC2478. It contains
 *                the following directories:
 *                1. _init- low level initialization of CPU, peripherals init
 *                as MAM, PLL, fast I/O and clock for whole CPU modules
 *                2. _drivers- API for LPC2478 peripherals
 *                3. _utils - utilities like printf, itoa, etc
 * Project:       HTX
 * Target:        LPC2478
 * Compiler:      arm-none-eabi-gcc
 * Date:          2010-12-02
 * Author:        Kuba
 * Based on: -
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include <stdbool.h>
# include "LPC24xx.h"
# include "typedef.h"
# include "init.h"
# include "irq.h"
# include "board.h"
# include "timer.h"
# include "uart.h"    /* it's an output for tests with Olimex board */
# include "printf.h"
# include "gpdma.h"
# include "ssp.h"

/*==============================================================================
 Defines
==============================================================================*/
# define SSP_TRANSFER_SIZE    149
# define SWITCH_DIR           FIO0DIR        //KEY2 P0.6 OR P0.29
# define SWITCH_PIN           FIO0PIN
# define SWITCH               (1 << 29)

/*==============================================================================
 Globals
==============================================================================*/
struct uart u0;
static struct timerConf tmr;
static timer t;

static struct sspConf ssp0;
static struct sspConf ssp1;

static uint16 ssp0TxBuff [SSP_TRANSFER_SIZE];
static uint16 ssp0RxBuff [SSP_TRANSFER_SIZE];
static uint16 ssp1TxBuff [SSP_TRANSFER_SIZE];
static uint16 ssp1RxBuff [SSP_TRANSFER_SIZE];
/*==============================================================================
 Static function prototypes
==============================================================================*/
static void jtagDelay   (void);
static void timerService(void);

static void setSsp0Tx (void);
static void setSsp1Tx (void);
static void testSsp0Rx (void);
static void testSsp1Rx (void);

static void ssp0TxTest  (void);
static void ssp0RxTest  (void);
static void ssp1TxTest  (void);
static void ssp1RxTest  (void);
static void switchInit  (void);
static uint8 switchPressed(void);
static void dummyService(void);
/*==============================================================================
 Main function
==============================================================================*/
int main(void)
{
   /* at the beginning of the main, the following functions are called:
    * - jtagDelay ();   delay necessary for JTAG proper work,
    * - systemInit ();  this function sets MAM (Memory Accelerator Module), PLL,
    *                   fast I/O, maximum speed for peripherals, interrupt vector
    */
   jtagDelay();
   systemInit ();
   vicInit();
   irqEnableCpsr();

   /* PERIPHERALS INITIALIZATION */
   /* uart */
   u0.uartNo            = uart0;
   u0.baud              = baud9600;
   u0.wordLen           = word8BIT;
   u0.stopBit           = stop1BIT;
   u0.parity            = parityDISABLE;
   u0.paritySel         = parityODD;
   u0.breakCtrl         = breakCTRL_DISABLE;
   u0.fifo              = 0;
   u0.intrpt            = 0;
   u0.intrptTxTrig      = 0;
   u0.intrptRxTrig      = 0;
   u0.buffers.txBuff    = 0;
   u0.buffers.txBuffSize= 0;
   u0.buffers.rxBuff    = 0;
   u0.buffers.rxBuffSize= 0;
   uartInit (&u0);   /* messages via RS232 */
   uprintf("%s\n", "ssp");

   /* leds */
   ledInit();
   /* timer */
   tmr.number = timer0;
   tmr.priority = irqLOWEST_PRIORITY;
   tmr.match[1].interrupt = matchINTERRUPT_ENABLE;
   tmr.match[1].reset = matchRESET_ENABLE;
   tmr.match[1].delay = 72000000;
   tmr.match[1].matchService = timerService; /* blink led */
   t = timerInit (&tmr);
   timerMatchEnable(t);
   /******************/
   /*****SSP test*****/
   /******************/
   switchInit();
   setSsp0Tx();
   setSsp1Tx();
   gpdmaInit(irqLOWEST_PRIORITY);
# if PM
   ssp1.priority     = irqLOWEST_PRIORITY;
   ssp1.mode         = sspMASTER;
   ssp1.frmSize      = sspFRAME_16BITS;
   ssp1.dma          = sspDMA_ENABLE;
   ssp1.txService    = ssp1TxTest;
   ssp1.rxService    = ssp1RxTest;
   ssp1.errorService = dummyService;
   ssp1Init(&ssp1);
   ssp1DmaInit();
   //ssp1DmaEnable(ssp1TxBuff, ssp1RxBuff, SSP_TRANSFER_SIZE);
# elif PS
   ssp1.priority  = irqLOWEST_PRIORITY;
   //sspm.port      = ssp1;
   ssp1.mode      = sspMASTER;
   ssp1.frmSize   = sspFRAME_16BITS;
   ssp1.dma       = sspDMA_ENABLE;
   ssp1.txService = ssp1TxTest;
   ssp1.rxService = ssp1RxTest;
   ssp1.errorService = dummyService;
   ssp1Init(&ssp1);
   ssp1DmaInit();
# elif PG
   ssp0.priority     = irqLOWEST_PRIORITY;
   ssp0.mode         = sspSLAVE;
   ssp0.frmSize      = sspFRAME_16BITS;
   ssp0.dma          = sspDMA_ENABLE;
   ssp0.txService    = ssp0TxTest;
   ssp0.rxService    = ssp0RxTest;
   ssp0.errorService = dummyService;
   ssp0Init(&ssp0);
   ssp0DmaInit();
   ssp0DmaEnable(ssp0TxBuff, ssp0RxBuff, SSP_TRANSFER_SIZE);
# endif
   uint32 i;

   while (1)
   {
# if PM
      if (switchPressed() == 1)
      {
         ssp1DmaEnable(ssp1TxBuff, ssp1RxBuff, SSP_TRANSFER_SIZE);
      }
# elif PS
      if (switchPressed() == 1)
      {
         ssp1DmaEnable(ssp1TxBuff, ssp1RxBuff, 20);
      }
# elif PG
//      if (switchPressed() == 1)
//      {
//         ssp0DmaEnable(ssp0TxBuff, ssp0RxBuff, 20);
//      }
# endif
//      ledOn(1);
//      for (i = 0; i < 500000; ++i);
//      ledOff(1);
//      for (i = 0; i < 500000; ++i);
   }
   return 0;
}

/*==============================================================================
 Static function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:      jtagDelay
 description:     function provides a delay required by jtag
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void jtagDelay (void)
{
   volatile unsigned int i;
   for (i = 0; i < 200000; ++i);
}

/*------------------------------------------------------------------------------
 function name:   timerService
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void timerService (void)
{
   //ledBlink(1);          /* change LED pin's state */
}

/*------------------------------------------------------------------------------
 function name:   setSsp0Tx
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void setSsp0Tx (void)
{
   int i;

   for (i = 0; i < SSP_TRANSFER_SIZE; ++i)
   {
      ssp0TxBuff[i] = 0x3522;
   }
}

/*------------------------------------------------------------------------------
 function name:   setSsp1Tx
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void setSsp1Tx (void)
{
   int i;

   for (i = 0; i < SSP_TRANSFER_SIZE; ++i)
   {
      ssp1TxBuff[i] = 0x1111;
   }
}

/*------------------------------------------------------------------------------
 function name:   testSsp0Rx
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void testSsp0Rx (void)
{
   int i;

   for (i = 0; i < SSP_TRANSFER_SIZE; ++i)
   {
      if (ssp0RxBuff[i] != 0x1111)
      {
         while(1);
      }
   }
}

/*------------------------------------------------------------------------------
 function name:   testSsp1Rx
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void testSsp1Rx (void)
{
   int i;

   for (i = 0; i < SSP_TRANSFER_SIZE; ++i)
   {
      if (ssp1RxBuff[i] != 0x3522)
      {
         while(1);
      }
   }
}

/*------------------------------------------------------------------------------
 function name:   ssp0TxTest
 description:     test function for SSP ISR service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void ssp0TxTest (void)
{
   return;
}

/*------------------------------------------------------------------------------
 function name:   ssp0RxTest
 description:     test function for SSP ISR service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void ssp0RxTest (void)
{
   testSsp0Rx();
   ledBlink(0);
   ssp0DmaEnable(ssp0TxBuff, ssp0RxBuff, SSP_TRANSFER_SIZE);
}

/*------------------------------------------------------------------------------
 function name:   ssp1TxTest
 description:     test function for SSP ISR service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void ssp1TxTest (void)
{
   return;
}

/*------------------------------------------------------------------------------
 function name:   ssp1RxTest
 description:     test function for SSP ISR service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void ssp1RxTest (void)
{
   testSsp1Rx();
   ledBlink(0);
   //ssp1DmaEnable(ssp1TxBuff, ssp1RxBuff, SSP_TRANSFER_SIZE);

}

/*------------------------------------------------------------------------------
 function name:   switchInit
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void switchInit (void)
{
   SWITCH_DIR &= ~SWITCH;
}

/*------------------------------------------------------------------------------
 function name:   switchPressed
 description:     check if switch is pressed
 parameters:      none
 returned value:  1 if pressed, 0 if not
------------------------------------------------------------------------------*/
static uint8 switchPressed (void)
{
   volatile uint32 i;
   if ((SWITCH_PIN & SWITCH) == 0)
   {
      for (i = 0; i < 800000; ++i);
      return 1;
   }
   else
   {
      return 0;
   }
}

/*------------------------------------------------------------------------------
 function name:   dummyService
 description:     just return
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void dummyService(void)
{
   return;
}
/******************************************************************************
* END OF FILE
******************************************************************************/
